package com.example.ch03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_sum;
    private ImageView iv;
    private Button btn_xrq,btn_xyc,btn_xrk;
    private Map<String,String>map;
    private List<Map<String,String>>list;
    private String sum;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
initview();

//        btn_xrq.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                iv.setImageResource(R.drawable.xrq);
////                tv_sum.setText("aa");
////                Toast.makeText(MainActivity.this,"仙人球",Toast.LENGTH_SHORT).show();
////                Log.i("a","仙人球");
//                getMap(0,R.drawable.xrq);
//            }
//        });

        try{
            //读取information文件
            InputStream is=this.getResources().openRawResource(R.raw.information);
            List<Flower> flowers=flowerservice.getFromXML(is);
//            String[] strs=new String[flowers.size()];
//            flowers.toArray(strs);
//
////            tv_sum.setText(sb.toString());

            list=new ArrayList<Map<String, String>>();
            for(Flower flower:flowers){
                map=new HashMap<String, String>();
                map.put("sum",flower.getSum());
                list.add(map);
            }

        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(this,"解析失败",Toast.LENGTH_SHORT).show();
            Log.i("a","解析失败");
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_xrq:
                getMap(0,R.drawable.xrq);
                break;
            case R.id.btn_xyc:
                getMap(1,R.drawable.xyc);
                break;
            case R.id.btn_xrk:
                getMap(2,R.drawable.xrk);
                break;
        }
    }

    private void initview(){
        tv_sum=(TextView)findViewById(R.id.tv_sum);
        iv=(ImageView)findViewById(R.id.iv);

        btn_xrq=(Button)findViewById(R.id.btn_xrq);
        btn_xyc=(Button)findViewById(R.id.btn_xyc);
        btn_xrk=(Button)findViewById(R.id.btn_xrk);
        btn_xrq.setOnClickListener(this);
        btn_xrk.setOnClickListener(this);
        btn_xyc.setOnClickListener(this);
    }
    private void getMap(int number,int iconNumber){
        Map<String,String> map=list.get(number);
        sum=map.get("sum");
        tv_sum.setText(sum);
        iv.setImageResource(iconNumber);
    }
}